﻿function GetUnicArray(inpurArray) {
    var i = inpurArray.length, result = [];
    inpurArray.sort(function (a, b) {return b - a;});
    while (i--) {
        if (result.join().search(inpurArray[i] + '\\b') == '-1') {
            result.push(inpurArray[i]);
        }
    }
    return result;
}

function stringFormat(args) {
    var str = this.toString();
    var searchIndex = 0;
    var counter = 0;

    do {
        searchIndex = str.indexOf("{%}", searchIndex);
        if (searchIndex > -1 && args[counter]) {
            str = str.substring(0, searchIndex) + args[counter] + str.substring(searchIndex + 3, str.length);
            searchIndex += args[counter++].length;
        } else {
            break;
        }
    } while (true);

    return str;
};
String.prototype.format = stringFormat;

// Проверка, является ли нажатие клавиши мыши правым кликом.
function isRightClick(event) {
    var rightclick = false;
    event = event || window.event;
    if (event.which) {
        rightclick = (event.which == 3); // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
    } else if (event.button) {
        rightclick = (event.button == 2); // IE, Opera
    }
    return rightclick;
}

$(window).resize(function () {
    $('#center').height($('#body1').height() - 130);
});
$(document).ready(function () {
    // Create a jqxMenu
    $("#MainMenu").jqxMenu({ theme: theme, width: '100%', height: '30', mode: 'horizontal', showTopLevelArrows: true });
    // Set up the open directions.
    $("#MainMenu").css('visibility', 'visible');
    
    $('#alert').jqxWindow({
        resizable: false,
        theme: theme,
        autoOpen: false, isModal: true,
        height: 'auto', width: '250px',
        initContent: function () {
            $("#okAlertButton").jqxButton({ width: '50px', theme: theme });
            $("#okAlertButton").bind('click', function () {
                $('#alert').jqxWindow('close');
            });
        }
    });
    window.alert = function (text, title) {
        if (title != null && title != "") {
            $('#alert').jqxWindow({ title: title });
        } else {
            $('#alert').jqxWindow({ title: 'Сообщение' });
        }
        var q = '';
        if (text != null && text.Message != null) {
            q = text.Message;
        }
        else {
            q = text;
        }
        $('#alert').jqxWindow('setContent', q + '<div id="okAlertButton">Ок</div>');

        $('#alert').jqxWindow('open');
    };
});

//функция обновляет дерево по полученным данным
function bindArrayToTree(treeId, data) {
    var source = {
        datatype: 'json',
        datafields: [
            { name: 'Id' },
            { name: 'ParentId' },
            { name: 'Name' }
        ],
        localdata: data
    };
    // create data adapter.
    var curDataAdapter = new $.jqx.dataAdapter(source);
    // perform Data Binding.
    curDataAdapter.dataBind();
    var records = curDataAdapter.getRecordsHierarchy('Id', 'ParentId', 'items', [{ name: 'Name', map: 'label' }, { name: 'Id', map: 'id' }]);
    $(treeId).jqxTree({
        source: records
    });
}

function isRightClick(event) {
    var rightclick;
    if (!event) var event = window.event;
    if (event.which) rightclick = (event.which == 3);
    else if (event.button) rightclick = (event.button == 2);
    return rightclick;
}
//определяет высоту контента
function CurrentHeigth() {
    return $(window).height() - 130;
}